// DOM元素
const winnerNameElement = document.getElementById('winnerName');
const funnyTextElement = document.getElementById('funnyText');
const anotherBtn = document.getElementById('anotherBtn');
const confettiContainer = document.getElementById('confetti-container');

// 幽默文本列表
const funnyTexts = [
    "恭喜你成为今天的主角！🌟",
    "被选中意味着你很特别，但也可能意味着今天要请客吃饭哦！😋",
    "恭喜你赢得了回答问题的机会，快乐双倍！😂",
    "你的运气真好，不过今天的幸运值可能已经用完了！😅",
    "被选中不要紧张，深呼吸，你可以的！💪",
    "哇！我们找到的未来的明星！✨",
    "看来今天的幸运女神/男神眷顾了你！🍀",
    "恭喜你获得了特别任务，表现出众哦！🎯",
    "这一刻，所有人的目光都是你的啦！👀",
    "命运之轮停在了你这里，是注定的缘分！🎡"
];

// 获取URL参数
function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    const regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    const results = regex.exec(location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

// 创建纸屑效果
function createConfetti() {
    const colors = ['#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', 
                    '#2196f3', '#03a9f4', '#00bcd4', '#009688', '#4CAF50', 
                    '#8BC34A', '#CDDC39', '#FFEB3B', '#FFC107', '#FF9800', '#FF5722'];
    
    for (let i = 0; i < 100; i++) {
        const confetti = document.createElement('div');
        confetti.style.position = 'absolute';
        confetti.style.width = `${Math.random() * 10 + 5}px`;
        confetti.style.height = `${Math.random() * 5 + 5}px`;
        confetti.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
        confetti.style.left = `${Math.random() * 100}%`;
        confetti.style.top = '-20px';
        confetti.style.opacity = Math.random() + 0.5;
        confetti.style.transform = `rotate(${Math.random() * 360}deg)`;
        confetti.style.borderRadius = `${Math.random() > 0.5 ? '50%' : '0'}`;
        
        confettiContainer.appendChild(confetti);
        
        // 动画
        setTimeout(() => {
            confetti.style.transition = `all ${Math.random() * 3 + 2}s linear`;
            confetti.style.top = '100%';
            confetti.style.transform = `rotate(${Math.random() * 360}deg) translateX(${Math.random() * 100 - 50}px)`;
        }, 0);
        
        // 移除
        setTimeout(() => {
            confetti.remove();
        }, 5000);
    }
}

// 页面加载
window.addEventListener('load', () => {
    // 从URL获取选中的人名
    const selectedName = getUrlParameter('name');
    
    if (selectedName) {
        winnerNameElement.textContent = selectedName;
        
        // 随机选择一条幽默文本
        const randomText = funnyTexts[Math.floor(Math.random() * funnyTexts.length)];
        funnyTextElement.textContent = randomText;
        
        // 创建纸屑效果
        createConfetti();
        
        // 每隔几秒创建新的纸屑
        setInterval(createConfetti, 3000);
    } else {
        // 如果没有获取到名字，返回主页
        window.location.href = 'index.html';
    }
});

// 再抽一次按钮
anotherBtn.addEventListener('click', () => {
    window.location.href = 'index.html';
}); 